
import $api from "@/api/index.js"
import $i18n from "@/utils/i18n" // 多语言
export default {
	state: {
		param: {
			page: 1,
			state: 1,
			category_id:'',
			// type:''
		},
		list: {
			data: [],
			last_page: 1,
			current_page: 1
		},
		OrderSteps:[{
			title: $i18n.tc('goods_order.step-1'),
		},{
			title: $i18n.tc('goods_order.step-2')
		},{
			title: $i18n.tc('goods_order.step-3')
		}],
		goodsDetailKey:'goode_detail_',
		orderListabs:[{
			name: $i18n.tc('my_goods_order.navs-all'),
		},{
			name: $i18n.tc('my_goods_order.navs-pay'),
			state:'1'
		},{
			name: $i18n.tc('my_goods_order.navs-send'),
			state:'2'
		},{
			name: $i18n.tc('my_goods_order.navs-take'),
			state:'3'
		},{
			name: $i18n.tc('my_goods_order.navs-success'),
			state:'4'
		}],
		orderParam: {
			page: 1,
			state: '',
		},
		orderList: {
			data: [],
			last_page: 1,
			current_page: 1
		},
		cateParam:{
			limit_size:50
		},
		cateList:[]
	},
	mutations: {
		// 更新状态
		updateMallState(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
		}
	},
	actions: {
		
		async getMallCate({
			commit,
			state
		},param){
			let res = await $api.mall.getMallCats(param)
			let cate = []
			res.data.forEach((cat) => {
				let {
					id,
					title,
					thumb_src,
					goods_count
					// sort
				} = cat
				cate.push({
					name:title,
					thumb:thumb_src,
					cid:id,
				});
			})
			commit('updateMallState', {
				key: 'cateList',
				val: cate
			})
		},
		
		// 获取商品列表
		async getGoodsList({
			commit,
			state
		}, param) {
			let res = await $api.mall.getMallList(param);
			// console.log(res)
			let oldList = state.list;
			let newList = res.data;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data) // 合并数组
				list = newList;
			}
			commit('updateMallState', {
				key: 'list',
				val: list
			})
		},
		
		// 获取我的订单列表
		async getMallMyOrderList({
			commit,
			state
		}, param) {
			let res = await $api.mall.getMallOrderList(param);
			// console.log(res)
			let oldList = state.orderList;
			let newList = res.data;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data) // 合并数组
				list = newList;
			}
			commit('updateMallState', {
				key: 'orderList',
				val: list
			})
		},
	}
}